package com.wh.wisdomsite.xbox.st.service.impl;

import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wh.wisdomsite.xbox.ba.dao.SysSequenceMapper;
import com.wh.wisdomsite.xbox.core.XboxException;
import com.wh.wisdomsite.xbox.st.dao.StoreInRecordMapper;
import com.wh.wisdomsite.xbox.st.entity.StoreInRecord;
import com.wh.wisdomsite.xbox.st.service.IPurchaseApplyService;
import com.wh.wisdomsite.xbox.st.service.IStoreInRecordService;
import com.wh.wisdomsite.xbox.utils.Const;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 出库记录serviceImpl
 * @version V1.0
 * @author: 杨世强
 * @Package com.wh.wisdomsite.xbox.st.service.impl
 * @date 2018-12-24 14:43
 * @Copyright 企业版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2018
 */
@Service("storeInRecordService")
@Transactional(rollbackFor = {Exception.class,RuntimeException.class})
public class StoreInRecordServiceImpl extends ServiceImpl<StoreInRecordMapper,StoreInRecord> implements IStoreInRecordService {
    private static final Log logger = LogFactory.getLog(StoreInRecordServiceImpl.class);
    @Autowired
    private StoreInRecordMapper storeInRecordMapper;

    @Autowired
    private SysSequenceMapper sysSequenceMapper;

    @Autowired
    private IPurchaseApplyService iPurchaseApplyService;

    @Override
    public Map<String, Object> getMapById(Serializable id) {
        return storeInRecordMapper.getMapById(id);
    }

    @Override
    public Page<Map<String, Object>> getMapsPage(Page page, Wrapper<StoreInRecord> wrapper) {
        SqlHelper.fillWrapper(page,wrapper);
        page.setRecords(this.baseMapper.getMapsPage(page,wrapper));
        return page;
    }

    @Override
    public Page<Map<String, Object>> getRecordPage(Page page, Wrapper<StoreInRecord> wrapper) {
        SqlHelper.fillWrapper(page,wrapper);
        page.setRecords(this.baseMapper.getRecordPage(page,wrapper));
        return page;
    }

    @Override
    public String getStiCodeByType(String type, String coCodeNum) {
        //入库单号
        String resultCode = "";
        //查询所需对应的序列
        String seqName = "";
        //合同内入库
        if(type.equals("RKN")){
            seqName = Const.STICODE_RKN_SEQ;
        }
        //合同外入库
        if(type.equals("RKW")){
            seqName = Const.STICODE_RKW_SEQ;
        }
        //填写入库
        if(type.equals("RKT")){
            seqName = Const.STICODE_RKT_SEQ;
        }
        Map<String,Object> parmaMap = new HashMap<>();
        parmaMap.put("seqName",seqName);
        parmaMap.put("seqValue",null);
        sysSequenceMapper.getSeqNextVal(parmaMap);
        Object valSeq = parmaMap.get("seqValue");
        if(valSeq == null){
            throw new XboxException("入库单序列为空，保存失败");
        }
        //根据序列名称获取序列值
        String valSeqStr = String.valueOf(valSeq);
        //计算序列长度，依据长度值给定前缀
        String seqNumLen = String.valueOf(valSeqStr.length());
        String numberFront = iPurchaseApplyService.getNumberFront(seqNumLen);
        //当前入库次数
        String curTime = numberFront + valSeqStr;
        //初始化物料入库次数
        String curRecord = "0001";
        resultCode = type + "_" + coCodeNum + "_" + curTime + "_" + curRecord;
        return resultCode;
    }

    /**
     * 获取最大编号
     * @param number
     * @return
     */
    @Override
    public String getMaxNumber(String number){
        String recordResult = null;
        //对最大记录加一操作
        Integer recordInt = Integer.valueOf(number);
        Integer maxRecordInt = recordInt + 1;
        String maxRecordStr = String.valueOf(maxRecordInt);
        int recoredFigure = maxRecordStr.length();
        //根据记录值位数添加0
        switch (recoredFigure){
            case 1:
                recordResult = "000"+maxRecordInt;
                break;
            case 2:
                recordResult = "00"+maxRecordInt;
                break;
            case 3:
                recordResult = "0"+maxRecordInt;
                break;
            case 4:
                recordResult = ""+maxRecordInt;
                break;
            default: recordResult = "0001";
                break;
        }
        return recordResult;
    }
}
